#ifndef __CC_MEMPOOL_H__
#define __CC_MEMPOOL_H__

#include "cc_list.h"
#include "cc_cond.h"
#include "cc_mutex.h"


typedef struct cc_mempool_block
{
  struct cc_mempool_block* next;
} cc_mempool_block;
typedef struct memory_pool
{
  cc_mempool_block* free_list;
  cc_mempool_block* alloc_list;
  cc_mutex mutex;
  cc_cond cond;
  void* mem_addr;
} cc_mempool, *cc_mempool_t;

#define MEM_BLOCK_REAL_SIZE(sz)          (sizeof(struct cc_mempool_block) + sz)
#define MEMPOOL_TOTAL_REAL_SIZE(num, sz) (num) * (MEM_BLOCK_REAL_SIZE(sz))
/**
 * @brief dynamic create a mempool
 *
 * @param block_num the block num of the mempool
 * @param blocksize the block size
 * @return mempool pool pointer
 *
 */
cc_mempool_t cc_mempool_create(size_t block_num, size_t block_size);
/**
 * @brief static create a mempool
 *
 * @param mem the mem pool pointer
 * @param data the mempool data
 * @param sz the data size
 * @param blocksize the every block size
 *
 */
int cc_mempool_init(cc_mempool_t pool, void* smem, size_t sz, size_t block_size);
/**
 * @brief destroy a mempool
 *
 * @param mem the mem pool pointer
 * @return error_code
 */
int cc_mempool_destroy(cc_mempool_t pool);
/**
 * @brief deinit a mempool
 *
 * @param mem the mem pool pointer
 * @return error_code
 */
int cc_mempool_deinit(cc_mempool_t pool);
/**
 * @brief get a block mem from the pool
 *
 * @param mem the mem pool pointer
 * @param timeout_ms delay_time
 * @return data_address
 */

void* cc_mempool_alloc_time(cc_mempool_t pool, uint32_t timeout);
void* cc_mempool_try_alloc(cc_mempool_t pool);
void* cc_mempool_alloc(cc_mempool_t pool);
/**
 * @brief free a block mem to the pool
 *
 * @param mem the mem pool pointer
 * @param m data_address
 * @return error code
 */
int cc_mempool_free(cc_mempool_t pool, void* m);

#endif    //__RF_MEMPOOL_H__
